package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CustomerBOInterface;
import org.cuatrovientos.erp.business.PayrollBOInterface;
import org.cuatrovientos.erp.model.Customer;
import org.cuatrovientos.erp.model.Payroll;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Customer management
 * 
 * @author VB
 */
@Controller
public class PayrollController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(PayrollController.class);

	@Autowired
	private PayrollBOInterface payrollBO;

	/**
	 * default constructor
	 */
	public PayrollController() {
		logger.info("Payroll BEAN instantiated");
	}

	/**
	 * handles default /payroll or /payroll/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer","/customer/list" )
	 */
	@RequestMapping({ "/payroll", "/payroll/list" })
	public ModelAndView payrollDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Payroll> payrollList = payrollBO.selectAll();
		model.put("payroll", payrollList);
		return new ModelAndView("PayrollList", model);
	}

	/**
	 * handles /payroll/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/payroll/search" })
	public ModelAndView search(@RequestParam(value = "description", defaultValue = "", required = true) String description) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Payroll> payrollList = payrollBO.search(description);
		model.put("payroll", payrollList);

		return new ModelAndView("PayrollList", model);
	}

	/**
	 * handles delete payroll, and redirect to payroll default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll","/payroll/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/payroll/delete" })
	public String deletePayroll(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		payrollBO.deleteById(id);
		return "redirect:/payroll";
	}

	/**
	 * handles new payroll request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "payroll"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/payroll/new" })
	public ModelAndView newPayroll() {
		logger.info("new payroll ");
		Payroll payroll = new Payroll();

		return new ModelAndView("NewPayroll", "payroll", payroll);
	}

	/**
	 * handles new payroll request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/payroll/save" })
	public String newPayroll(@Valid Payroll payroll,BindingResult bindingResult) {
		logger.info("new payroll " + payroll.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("payroll", payroll);
			return "NewPayroll";
		}

		// If data is ok, insert and go on
		payrollBO.insert(payroll);
		return "redirect:/payroll";
	}

	/**
	 * handles delete payroll, and redirect to payroll default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll","/payroll/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/payroll/detail" })
	public ModelAndView customerDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Payroll payroll = payrollBO.selectById(id);
		model.put("payroll", payroll);

		return new ModelAndView("PayrollDetail", model);
	}

	/**
	 * handles payroll update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/payroll/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Payroll payroll = payrollBO.selectById(id);
		model.put("payroll", payroll);
		return new ModelAndView("UpdatePayroll", model);

	}

	/**
	 * handles save update payroll request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/payroll/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/payroll/saveupdate" })
	public String saveUpdate(@Valid Payroll payroll,BindingResult bindingResult) {
		logger.info("update payroll " + payroll.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("payroll", payroll);
			return "UpdatePayroll";
		}

		payrollBO.update(payroll);
		return "redirect:/payroll";
	}
}
